package com.example.smartcity.ui.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.example.smartcity.R;

import java.util.ArrayList;

public class SmartBarExListAdapter extends BaseExpandableListAdapter {
    private ArrayList<ArrayList<String>> group;
    private ArrayList<ArrayList<String>> item;
    public SmartBarExListAdapter(ArrayList<ArrayList<String>> group,ArrayList<ArrayList<String>> item){
        this.group = group;
        this.item = item;
    }
    @Override
    public int getGroupCount() {
        return group.get(0).size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return item.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return item.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = null;
        GroupViewHolder groupViewHolder = null;
        if (convertView==null){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlistgrouplayout,parent,false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.TV_end = view.findViewById(R.id.TV_end);
            groupViewHolder.TV_endTime = view.findViewById(R.id.TV_endTime);
            groupViewHolder.TV_first = view.findViewById(R.id.TV_first);
            groupViewHolder.TV_mileage = view.findViewById(R.id.TV_CSmileage);
            groupViewHolder.TV_name = view.findViewById(R.id.TV_name);
            groupViewHolder.TV_price = view.findViewById(R.id.TV_CSprice);
            groupViewHolder.TV_StartTime = view.findViewById(R.id.TV_StartTime);
            view.setTag(groupViewHolder);
        }else{
            view = convertView;
            groupViewHolder = (GroupViewHolder) view.getTag();
        }
        groupViewHolder.TV_name.setText(group.get(0).get(groupPosition));
        groupViewHolder.TV_first.setText(group.get(1).get(groupPosition));
        groupViewHolder.TV_end.setText(group.get(2).get(groupPosition));
        groupViewHolder.TV_price.setText(group.get(3).get(groupPosition));
        groupViewHolder.TV_mileage.setText(group.get(4).get(groupPosition));
        groupViewHolder.TV_StartTime.setText(group.get(5).get(groupPosition));
        groupViewHolder.TV_endTime.setText(group.get(6).get(groupPosition));
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view =null;
        itemViewHolder itemViewHolder =null;
        if (convertView==null){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlistitemlayout,parent,false);
            itemViewHolder = new itemViewHolder();
            itemViewHolder.TV_itemName = view.findViewById(R.id.TV_itemName);
            itemViewHolder.TV_state = view.findViewById(R.id.TV_state);
            view.setTag(itemViewHolder);
        }else{
            view = convertView;
            itemViewHolder = (SmartBarExListAdapter.itemViewHolder) view.getTag();
        }
        //设置子项数据
        itemViewHolder.TV_itemName.setText(item.get(groupPosition).get(childPosition));
        if (itemViewHolder.TV_itemName.getText().equals(item.get(groupPosition).get(0))){
            itemViewHolder.TV_state.setText("起点");
        }
        if (itemViewHolder.TV_itemName.getText().equals(item.get(groupPosition).get(item.get(groupPosition).size()-1))){
            itemViewHolder.TV_state.setText("终点");
        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    class GroupViewHolder{
        TextView TV_name;
        TextView TV_first;
        TextView TV_end;
        TextView TV_price;
        TextView TV_StartTime;
        TextView TV_mileage;
        TextView TV_endTime;
    }
    class itemViewHolder{
        TextView TV_state;
        TextView TV_itemName;
    }
}
